#!usr/bin/env python
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tap_connector15_dameng.py
@version:
@time: 2022/01/13 
"""
import dmPython

from TapClientConnector.connector import connector_query_logger, TAPOracleConnector, connector_exec_logger
from TapException.CONNECTOR import SQLError
from db_info import DM


class TAPDMConnector(TAPOracleConnector):
    def __init__(self, kwargs):
        super(TAPOracleConnector, self).__init__(kwargs)
        host = self.FORMAT_DB_INFO.pop('host')
        port = self.FORMAT_DB_INFO.pop('port')
        database = self.FORMAT_DB_INFO.pop('database')
        dsn = f'{host}:{port}'
        self.FORMAT_DB_INFO.update({'dsn': dsn})
        self.connector = dmPython.connect(**self.FORMAT_DB_INFO)

    def _query(self, sql):
        cursor = self.connector.cursor()
        try:
            cursor.execute(sql)
        except Exception:
            raise SQLError
        res = list(cursor.fetchall())
        self.table_description = cursor.description
        cursor.close()
        return res

    @connector_query_logger
    def show_tables(self) -> list:
        table_list = []
        for i in self._query("select table_name from user_tables"):
            table_list.append(i[0])
        return table_list

    @connector_exec_logger
    def _exec(self, sql):
        cursor = self.connector.cursor()
        try:
            cursor.execute(sql)
        except Exception:
            raise SQLError


# conn = dmPython.connect(user='sysdba', password='Gotapd8!', server='192.168.1.183', port=52360)
# cursor = conn.cursor()
# cursor.execute('select username from dba_users')
# values = cursor.fetchall()
# print(values)
# cursor.close()
# conn.close()
if __name__ == '__main__':
    from TapDataSourceInfo.db_info_pdk import

    t = TAPDMConnector(DM.dm_source_test)
    t.show_dbs()
    t.show_tables()
